<?php

namespace addons\Flow\api\modules\v1\controllers;

use common\helpers\ArrayHelper;
use Yii;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use addons\Flow\common\models\Workflow;


class WorkflowController extends OnAuthController
{
    public $modelClass = Workflow::class;

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $authOptional = ['index', 'view'];

    /**
     * 首页
     *
     * @return ActiveDataProvider
     */
    public function actionIndex()
    {
        return new ActiveDataProvider([
            'query' => $this->modelClass::find()
                ->andWhere(['merchant_id' => Yii::$app->user->identity->merchant_id])
                ->orderBy('sort asc,id desc')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);

    }
    public function actionState()
    {
        return ArrayHelper::arrayToArrays(\addons\Flow\common\enums\WorkflowEnum::getMap());
    }
    public function actionRemind()
    {
        return ArrayHelper::arrayToArrays(\addons\Flow\common\enums\RemindTypeEnum::getMap());
    }
    protected function findModel($id)
    {
        if (empty($id) || !($model = $this->modelClass::findOne($id))) {
            throw new NotFoundHttpException('请求的数据不存在');
        }
        return $model;
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, [])) {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}